//
//  LogisticsCell.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2021/6/10.
//  Copyright © 2021 zwb. All rights reserved.
//

import UIKit
import Kingfisher

class ZyaLogisticsCell: UITableViewCell {
    
    private var lab1: UILabel!
    private var labStatus: UILabel!
//    private var labLogistics: UILabel!
    private var labTime: UILabel!
    private var labSignDate: UILabel!
    private var labTo: UILabel!
    private var labPayAmount: UILabel!
    private var labPaydAmount: UILabel!
    
    private lazy var periodLab: UILabel = {
        let lab = UILabel().easy(.black, .systemFont(ofSize: 18))
            .extNumberOfLines(lines: 2)
            .extTextAligment(aligment: .center)
        
        return lab
    }()

    lazy var orangeAttr: [NSAttributedString.Key : Any] = [NSAttributedString.Key.foregroundColor: UIColor(kshex: "#F7860F")]
    lazy var grayAttr: [NSAttributedString.Key : Any] = [NSAttributedString.Key.foregroundColor: UIColor(kshex: "#bababa")]
    
    lazy var bg: UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.masksToBounds = true
        v.layer.cornerRadius = 6
        return v
    }()
    
    lazy var headerBg: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(kshex:"#4BC354")
        return v
    }()
    
    // 10颜色：在途#4BC354；签收#F7860F；新增&保存#2FAA1B
    private(set) lazy var operateBtnGroup = OperateBtnGroup(frame: CGRect(x: 20, y: 0, width: KSGlobal.screenW - 40, height: 32), titles: [])
    
    var one = ZyaLogisticsModel(JSON:["":""])! {
        didSet {
            lab1.text = one.zType.unwrap + one.zContractNo.unwrap
            let oriS = "状态：运输中→已签收→付款中→已付款"
            let vAt = NSMutableAttributedString(string: oriS)
            var preS = ""
            var sufS = ""
            
            labSignDate.extHidden(hidden: true)
            labPayAmount.extHidden(hidden: true)
            labPaydAmount.extHidden(hidden: true)
            
            switch one.zStatus {
            case "0":
                preS = "运输中→"
                sufS = "已签收→付款中→已付款"
            case "1":
                preS = "运输中→已签收→"
                sufS = "付款中→已付款"
                
                labSignDate.extHidden(hidden: false)
                
            case "2":
                preS = "运输中→已签收→付款中→"
                sufS = "已付款"
                
                labSignDate.extHidden(hidden: false)
                labPayAmount.extHidden(hidden: false)
                
            case "3":
                preS = "运输中→已签收→付款中→已付款"
                sufS = ""
                
                labSignDate.extHidden(hidden: false)
                labPayAmount.extHidden(hidden: false)
                labPaydAmount.extHidden(hidden: false)
                
            default:
                break
            }
            
            vAt.addAttributes(orangeAttr, range: (oriS as NSString).range(of: preS))
            vAt.addAttributes(grayAttr, range: (oriS as NSString).range(of: sufS))
            labStatus.attributedText = vAt
            
            headerBg.backgroundColor = UIColor(kshex: "#4BC354")
            
//            if one.zDeliveryType == "1" {
//                labLogistics.text = "供应商自送"
//            } else {
//                labLogistics.text = one.zLogisticsNm.unwrap + "：" + one.zLogisticsNo.unwrap
//            }
            
            let consignerDate = one.zConsignerDate.unwrap
            if !consignerDate.isEmpty {
                labTime.text = "发货日期：\(consignerDate)"
            } else {
                labTime.text = "发货日期：无"
            }
          
            let signDate = one.zSignDate.unwrap
            if !signDate.isEmpty {
                labSignDate.text = "签收日期：\(signDate)"
            } else {
                labSignDate.text = "签收日期："
            }
            
            let payAmount = one.zPayAmount.unwrap
            if !payAmount.isEmpty {
                labPayAmount.text = "应付金额：\(payAmount)"
            } else {
                labPayAmount.text = "应付金额：无"
            }
            
            let paydAmount = one.zPaydAmount.unwrap
            if !paydAmount.isEmpty {
                labPaydAmount.text = "已付金额：\(paydAmount)"
            } else {
                labPaydAmount.text = "已付金额：无"
            }

            labTo.text = one.zCityFh.unwrap + "→" + one.zCitySh.unwrap
            
            
            if let value = one.zRemianPeriod {
                periodLab.extHidden(hidden: false)
                
                if value < 7 {
                    periodLab.textColor = .systemRed
                     
                } else if value < 30 {
                    periodLab.textColor = Theme.blue
                    
                } else {
                    periodLab.textColor = Theme.primaryColor()
                }
                
                let perAttrStr = NSMutableAttributedString(string: "\(value)天\n")
                
                let attrDict = [.foregroundColor: UIColor.darkGray, .font: UIFont.systemFont(ofSize: 12)] as [NSAttributedString.Key: Any]
                perAttrStr.append(NSAttributedString(string: "账期剩余", attributes: attrDict))
                
                periodLab.attributedText = perAttrStr
                
            } else {
                periodLab.extHidden(hidden: true)
            }
            
            var operateTitles = [String]()
            var operateColors = [UIColor]()

            if one.zDeliveryType == "1" {
                operateTitles.append(contentsOf: one.zStatus == "0" ? ["编辑",  "删除"] : ["查看", "删除"] )
            } else {
                operateTitles.append(contentsOf: one.zStatus == "0" ? ["编辑", "删除", "物流信息"] : ["查看", "删除", "物流信息"])
            }
            
            if one.zAccPeriod == 0 {
                operateTitles.append("催款")
                
                var index = 0
                // 处理有删除按钮的特殊情况
                let count = one.zStatus == "1" ? operateTitles.count : operateTitles.count - 1
                while index < count {
                    if index == count - 1 {
                        operateColors.append(.systemRed)
                    } else {
                        operateColors.append(Theme.primaryColor())
                    }
                    index += 1
                }
                
                operateBtnGroup.setTitle(operateTitles, colors: operateColors)

            } else {
                operateBtnGroup.setTitle(operateTitles)
            }
        }
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        makeUI()
    }
 
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension ZyaLogisticsCell {
    
    private func makeUI() {
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        contentView.addSubview(bg)
        bg.addSubview(headerBg)
        
        lab1 = UILabel()
        lab1.numberOfLines = 2
        lab1.font = UIFont.systemFont(ofSize: 15)
        lab1.textColor = .white
        headerBg.addSubview(lab1)
        
        labTo = UILabel()
        labTo.font = UIFont.systemFont(ofSize: 15)
        labTo.textColor = .white
        headerBg.addSubview(labTo)

        labStatus = UILabel()
        labStatus.numberOfLines = 0
        labStatus.font = UIFont.systemFont(ofSize: 14)
        labStatus.textColor = .darkGray
        bg.addSubview(labStatus)
        
//        labLogistics = UILabel()
//        labLogistics.textColor = .darkGray
//        labLogistics.font = UIFont.systemFont(ofSize: 14)
//        labLogistics.numberOfLines = 2
//        bg.addSubview(labLogistics)
        
        labTime = UILabel()
        labTime.font = UIFont.systemFont(ofSize: 14)
        labTime.textColor = .darkGray
        bg.addSubview(labTime)
        
        labSignDate = UILabel()
        labSignDate.textColor = .darkGray
        labSignDate.font = UIFont.systemFont(ofSize: 14)
        bg.addSubview(labSignDate)
        
        labPayAmount = UILabel()
        labPayAmount.textColor = .darkGray
        labPayAmount.font = UIFont.systemFont(ofSize: 14)
        bg.addSubview(labPayAmount)
        
        labPaydAmount = UILabel()
        labPaydAmount.textColor = .darkGray
        labPaydAmount.font = UIFont.systemFont(ofSize: 14)
        bg.addSubview(labPaydAmount)
        
        bg.snp.makeConstraints { (make) in
            make.edges.equalTo(UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5))
        }
        
        headerBg.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(30)
        }
        
        lab1.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(5)
            make.right.equalToSuperview().offset(-5)
            make.centerY.equalToSuperview()
        }
        
        labTo.snp.makeConstraints { (make) in
            make.trailing.equalToSuperview().offset(-12)
            make.centerY.equalToSuperview()
        }
        
//        labLogistics.snp.makeConstraints { (make) in
//            make.leading.equalTo(lab1)
//            make.top.equalTo(headerBg.snp.bottom).offset(6)
//        }
        
        labStatus.snp.makeConstraints { (make) in
            make.left.equalTo(lab1)
            make.top.equalTo(headerBg.snp.bottom).offset(6)
            make.right.equalToSuperview().offset(-5)
            make.height.equalTo(16)
        }
        
        labTime.snp.makeConstraints { (make) in
            make.left.right.height.equalTo(labStatus)
            make.top.equalTo(labStatus.snp.bottom).offset(6)
        }
        
        labSignDate.snp.makeConstraints { (make) in
            make.left.right.height.equalTo(labTime)
            make.top.equalTo(labTime.snp.bottom).offset(6)
        }
        
        labPayAmount.snp.makeConstraints { (make) in
            make.left.right.height.equalTo(labSignDate)
            make.top.equalTo(labSignDate.snp.bottom).offset(6)
        }
        
        labPaydAmount.snp.makeConstraints { (make) in
            make.left.right.height.equalTo(labPayAmount)
            make.top.equalTo(labPayAmount.snp.bottom).offset(6)
        }
        
        bg.addSubview(operateBtnGroup)
        operateBtnGroup.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.height.equalTo(35)
            make.bottom.equalToSuperview().offset(-6)
        }
        
        bg.addSubview(periodLab)
        periodLab.snp.makeConstraints { make in
            make.width.equalTo(66)
            make.height.equalTo(40)
            make.bottom.equalTo(operateBtnGroup.snp.top).offset(-10)
            make.right.equalToSuperview().offset(-8)
        }
    }
    
}
